package me.evis.mobile.sms2cal.parser;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import me.evis.mobile.sms2cal.model.CalEvent;
import me.evis.mobile.sms2cal.model.SmsData;
import android.content.Context;
import android.util.Log;
import edu.fudan.nlp.chinese.ner.TimeNormalizer;
import edu.fudan.nlp.chinese.ner.TimeUnit;

public class NLPTimeParser implements SmsParser {
    /** Tag string for debug logs */
    private static final String TAG = "me.evis.mobile.sms2cal.parser.NLPTimeParser";
    
    private static final String NLP_MODEL_FILENAME = "nlp_models/time_exp.model";
    
//    private static final List<String> possibleSenders = Arrays.asList(new String[] {
//            "10086", "10010"
//    });
    
    private TimeNormalizer timeNormalizer;
    
    public NLPTimeParser(Context context) throws IOException {
        timeNormalizer = 
                new TimeNormalizer(context.getAssets().open(NLP_MODEL_FILENAME));
    }
    
    @Override
    public boolean parse(SmsData sms, List<CalEvent> results) {
        // Parse
        timeNormalizer.parse(sms.messageBody);
        TimeUnit[] unit = timeNormalizer.getTimeUnit();
        if (unit.length == 0) {
            return false;
        }
        
        for(int i = 0; i < unit.length; i++){
            
            CalEvent calEvent = new CalEvent();
            calEvent.dtStart = unit[i].toDate();
            calEvent.dtEnd = getDtEnd(calEvent.dtStart);
            calEvent.dtStamp = new Date();
            calEvent.summary = "Appointment from " + sms.from;
            calEvent.description = sms.messageBody;
            calEvent.categories = "APPOINTMENT";
            calEvent.sourceSms = sms;
            Log.d(TAG, calEvent.toString());
            results.add(calEvent);
        }
        return true;
    }
    
    private Date getDtEnd(Date dtStart) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dtStart);
        calendar.add(Calendar.HOUR, 1);
        return calendar.getTime();
    }
}
